package daemothread;

/**
 * @description:
 * @author: jhy
 * @time: 2021/4/12 13:44
 */

//守护线程：当所有用户线程结束，守护线程才退出，否则守护线程一直允许
public class DaemoThreadDemo1 {

    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        //将线程设置为守护线程
        myThread.setDaemon(true);
        myThread.start();
        Thread.sleep(5000);
        System.out.println("主线程退出，守护线程也不在打印了");

    }


}
class MyThread extends Thread{

    private int i = 0;

    @Override
    public void run() {
        try {
            while (true){
                i++;
                System.out.println("i = " + i);
                Thread.sleep(1000);
            }
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }
    }
}
